﻿using System;
using System.Windows.Forms;

namespace LevelEditor
{
	public partial class EventTimeline : UserControl
	{
		bool ranged;
		private struct GameEventItem
		{
			public Sudum.GameEvent.Type Type { get; set; }
			public GameEventItem(Sudum.GameEvent.Type eventType) : this() { Type = eventType; }
			public override string ToString() { return Sudum.GameEvent.Types[Type]; }
		};

		public Sudum.GameEvent.Type EventType
		{
			get { return ((GameEventItem)comboBoxEvent.SelectedItem).Type; }
			set
			{
				foreach (GameEventItem evt in comboBoxEvent.Items)
				{
					if (evt.Type == value)
					{
						comboBoxEvent.SelectedItem = evt;
						break;
					}
				}
			}
		}

		int spawnCount;
		public int SpawnCount
		{
			get { return spawnCount; }
			set
			{
				spawnCount = value;
				UpdateText();
			}
		}
		public bool Ranged { get { return ranged; } }
		public int StartTime
		{
			// لاحظ أن القيمة معكوسة، وذلك كي تعمل التحكمات من اليمين إلى اليسار
			get
			{
				if (ranged)
					return rangeBar.TotalMaximum - rangeBar.RangeMaximum;
				return trackBar.Maximum - trackBar.Value;
			}

			set
			{
				
				if (ranged)
					rangeBar.RangeMaximum = rangeBar.TotalMaximum - value;
				else trackBar.Value = trackBar.Maximum - value;
				UpdateText();
			}
		}
		public int EndTime
		{
			get
			{
				if (ranged)
					return rangeBar.TotalMaximum - rangeBar.RangeMinimum;
				return StartTime;
			}

			set
			{
				if (ranged)
					rangeBar.RangeMinimum = rangeBar.TotalMaximum - value;
				else StartTime = value;
				UpdateText();
			}
		}
		public event EventHandler Deleted;

		public EventTimeline(bool rangedTrack, int lengthInSeconds)
		{
			InitializeComponent();

			Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
			ranged = rangedTrack;

			foreach (var type in Sudum.GameEvent.Types.Keys)
				comboBoxEvent.Items.Add(new GameEventItem(type));
			comboBoxEvent.SelectedIndex = 0;

			// تخلص من التحكم الغير مستخدم
			Controls.Remove(ranged ? (Control)trackBar : (Control)rangeBar);
			if (ranged)
			{
				Controls.Remove(trackBar);
				trackBar = null;
			}
			else
			{
				Controls.Remove(rangeBar);
				rangeBar = null;
			}

			SetLength(lengthInSeconds, false);
		}

		public void SetLength(int lengthInSeconds, bool rescaleEvents)
		{
			if (ranged)
			{
				if (rescaleEvents)
				{
					// إعادة حساب الوقت كنسبة
					float startPercent = StartTime / (float)rangeBar.TotalMaximum;
					float endPercent = EndTime / (float)rangeBar.TotalMaximum;
					int startNew = (int)(startPercent * lengthInSeconds);
					int endNew = (int)(endPercent * lengthInSeconds);
					rangeBar.SetRangeLimit(0, lengthInSeconds);
					EndTime = endNew;
					StartTime = startNew;
				}
				else
				{
					// عدم إعادة حساب الوقت. فقط احجز الحدث عند آخر لحظة ممكنة إن لزم
					int startTime = (StartTime > lengthInSeconds) ? lengthInSeconds : StartTime;
					int endTime = (EndTime > lengthInSeconds) ? lengthInSeconds : EndTime;
					rangeBar.SetRangeLimit(0, lengthInSeconds);
					StartTime = startTime;
					EndTime = endTime;
					StartTime = startTime; // هذا السطر مكرر ليس بالخطأ، وإنما بسبب طريقة تصرف التحكم
				}
				rangeBar.DivisionNum = lengthInSeconds / 10;
			}
			else
			{
				if (rescaleEvents)
				{
					// إعادة حساب الوقت كنسبة
					float timePercent = StartTime / (float)trackBar.Maximum;
					int timeNew = (int)(timePercent * lengthInSeconds);
					trackBar.SetRange(0, lengthInSeconds);
					StartTime = timeNew;
				}
				else
				{
					// عدم إعادة حساب الوقت. فقط احجز الحدث عند آخر لحظة ممكنة إن لزم
					int time = (StartTime > lengthInSeconds) ? lengthInSeconds : StartTime;
					trackBar.SetRange(0, lengthInSeconds);
					StartTime = time;
				}
			}
			UpdateText();
		}

		void UpdateText()
		{
			if (ranged)
			{
				txtSummary.Text = "من الثانية " + StartTime + " إلى الثانية " + EndTime + "، بث عناصر بعدد " + SpawnCount;
			}
			else
			{
				txtSummary.Text = "في الثانية " + StartTime + "، بث عناصر بعدد " + SpawnCount;
			}
		}

		#region Events
		private void btnDeleteEvent_Click(object sender, EventArgs e)
		{
			Deleted(this,e); // إعلام المهتمين بأننا قد حُذفنا
		}

		private void btnSetEventProperties_Click(object sender, EventArgs e)
		{
			EventPropertiesDialog eventPropsDlg = new EventPropertiesDialog(SpawnCount);
			if (eventPropsDlg.ShowDialog() != DialogResult.OK)
				return;
			SpawnCount = eventPropsDlg.SpawnCount;
			UpdateText();
		}

		private void OnRangeChanging(object sender, EventArgs e)
		{
			UpdateText();
		}
		#endregion
	}
}
